package com.atguigu.gmall.order.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisDelayQueue {

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;

    @Autowired
    private RedissonClient redissonClient ;

    @PostConstruct
    public void init() {
        RBlockingQueue<Object> rBlockingQueue = redissonClient.getBlockingQueue("order_close_delay_queue");
        RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(rBlockingQueue);
        delayedQueue.offer("798768687687" , 10 , TimeUnit.SECONDS) ;
        delayedQueue.offer("879845187844" , 30 , TimeUnit.SECONDS) ;
    }

//    @PostConstruct              // 初始化方法
//    public void init() {
//        log.info("RedisDelayQueue...init...");
//        long task1 = System.currentTimeMillis() + 1000 * 30 ;
//        long task2 = System.currentTimeMillis() + 1000 * 60 ;
//        redisTemplate.opsForZSet().add("order:delay:queue" , "9868768768768767" , task1) ;
//        redisTemplate.opsForZSet().add("order:delay:queue" , "7845158781212345" , task2) ;
//    }

}
